package com.firefly.mobile.app.context.init;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;

import com.firefly.mobile.app.config.PackConfig;
import com.firefly.mobile.app.context.SytApplication;

/**
 * 业务包初始化工厂
 * 
 * @author Brain
 * 
 */
public class BizPackInitFactory {
	public void init(Context context) {
		try {
				String str[] = context.getAssets().list(SytApplication.BIZ_PACK_FOLDER_PATH);
				if (str == null || str.length == 0) {
					return;
				}
				for (String fileName : str) {
					InputStream is = null;
					try {
						is = context.getAssets().open(SytApplication.BIZ_PACK_FOLDER_PATH+ File.separator+fileName);
					} catch (Exception e) {
						e.printStackTrace();
						return;
					}
					List<PackConfig> list = parsePackConfigs(is);
					for (PackConfig config : list) {
						config.init();//初始化
					}
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static List<PackConfig> parsePackConfigs(InputStream is){
		try {
			List<PackConfig> list = new ArrayList<PackConfig>();
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 取得DocumentBuilderFactory实例
			factory.setIgnoringElementContentWhitespace(true);
			factory.setIgnoringComments(true);
			DocumentBuilder builder = factory.newDocumentBuilder(); // 从factory获取DocumentBuilder实例
			Document doc = builder.parse(is); // 解析输入流 得到Document实例
			Element rootEle = doc.getDocumentElement();
			
			//获取交易报文配置文件的class属性，然后初始化
//			String packClass = rootEle.getAttribute("class");
//			if(!StringUtils.isEmpty(packClass)){
//				@SuppressWarnings("rawtypes")
//				Class c = Class.forName(packClass);
//				c.newInstance();
//			}
			NodeList items = rootEle.getElementsByTagName("bizpack");
			for(int i=0;i<items.getLength();i++){
				Element bizpackItem=(Element)items.item(i);
				NodeList propItems = bizpackItem.getElementsByTagName("property");
				Map<String, String> map = new HashMap<String, String>();
				for(int j=0;j<propItems.getLength();j++){
					Element item=(Element)propItems.item(j);
					String key = item.getAttribute("name");
					String value = item.getAttribute("value");
					map.put(key, value);
				}
				transBizPackConfig(list, map);
			}
			
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static void transBizPackConfig(List<PackConfig> list, Map<String, String> map) {
		PackConfig config = new PackConfig();
		for (String key : map.keySet()) {
			if ("msgType".endsWith(key)) {
				config.setMsgType(map.get(key));
				continue;
			}
			if ("id".endsWith(key)) {
				config.setId(map.get(key));
				continue;
			}
			if ("cardKind".endsWith(key)) {
				config.setCardKind(map.get(key));
				continue;
			}
			if ("processNo".endsWith(key)) {
				config.setProcessNo(map.get(key));
				continue;
			}
			if ("packId".endsWith(key)) {
				config.setPackId(map.get(key));
			}
			if ("unPackId".endsWith(key)) {
				config.setUnPackId(map.get(key));
				continue;
			}
			if ("encryptTrack".endsWith(key)) {
				config.setEncryptTrack(Boolean.parseBoolean(map.get(key)));
				continue;
			}
			if ("payMode".endsWith(key)) {
				config.setPayMode(map.get(key));
				continue;
			}
			if ("bizMode".endsWith(key)) {
				config.setBizMode(map.get(key));
				continue;
			}
			if ("interfaceVer".endsWith(key)) {
				config.setInterfaceVer(map.get(key));
				continue;
			}
			if ("payRoute".endsWith(key)) {
				config.setPayRoute(map.get(key));
				continue;
			}
		}
		list.add(config);
	}

}
